package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * JDBC java数据库连接
 */
public class JDBCDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1.注册驱动Class.forName()
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2.建立连接DriverManager.getConnection()
        String url = "jdbc:mysql://localhost:3307/birdbootdb?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        // 3.创建Statement对象
        Statement statement = connection.createStatement();
        System.out.println("欢迎注册：");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        String age = scanner.nextLine();
        // 4.执行SQL
        String sql = "INSERT INTO userinfo (username, password, nickname, age) " +
                "VALUES ('" + username + "', '" + password + "', '" + nickname + "', " + age + ")";
        // 使用此方法来执行DML语句增删改
        statement.executeUpdate(sql);
        System.out.println("更新成功！");

        connection.close();
    }
}
